package com.xinyue.center.controller;

import com.xinyue.center.common.GameCenterConfig;
import com.xinyue.center.common.ServerResponse;
import com.xinyue.center.dao.entity.Account;
import com.xinyue.center.messages.request.LoginRequest;
import com.xinyue.center.messages.response.VoAccount;
import com.xinyue.center.service.GameGatewayService;
import com.xinyue.center.service.IAccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IAccountService accountService;
    @Autowired
    private GameGatewayService gameGatewayService;
    @Autowired
    private GameCenterConfig gameCenterConfig;

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @PostMapping("login")
    public ServerResponse<VoAccount> login(@RequestBody LoginRequest loginRequest, HttpServletRequest request) {

        String loginIp = request.getRemoteAddr();
        loginRequest.setLoginIp(loginIp);

        Account account = accountService.login(loginRequest);
        VoAccount userAccount = new VoAccount();
        BeanUtils.copyProperties(account, userAccount);
        String token = accountService.createUserToken(account);
        userAccount.setToken(token);
        return new ServerResponse<VoAccount>(userAccount);
    }

//    @PostMapping("createPlayer")
//    public ResponseEntity<VoGameRole> createPlayer(@RequestBody CreatePlayerParam param, HttpServletRequest request) throws TokenException {
//        param.checkParam();
//        String userToken = request.getHeader(GameConstants.USER_TOKEN);
//        TokenContent tokenContent = JWTUtil.getTokenContent(userToken);
//        long userId = tokenContent.getUserId();
//        PlayerBasic player = playerService.createPlayer(param, userId);
//        accountService.addPlayer(userId, player);
//        VoGameRole playerBasic = playerService.getVoPlayer(player);
//
//        ResponseEntity<VoGameRole> response = new ResponseEntity<VoGameRole>(playerBasic);
//        return response;
//    }


}
